package gxprod.androidai.functions;

import gxprod.androidai.AIActivity;
import gxprod.androidai.Bubble;
import gxprod.androidai.BubbleAdapter;
import gxprod.androidai.regex.MatchResult;
import gxprod.androidai.regex.NamedPattern;
import gxprod.androidai.widgets.WebWidget;
import android.net.Uri;

/**
 * Web searches, definitions, and pulling sniplets from wikipedia pages
 * @author Josh Maleszewski
 * @author Austin Bolstridge
 */
public class SearchFunction extends Function {

	public static final String HTTP						= "http://";

	public static final String SEARCH_GOOGLE	 		= HTTP + "www.google.com/search?q=";
	public static final String SEARCH_YAHOO		 		= HTTP + "search.yahoo.com/mobile/s?p=";
	public static final String SEARCH_YAHOO_ANSWERS		= HTTP + "answers.yahoo.com/search/search_result?p=";
	public static final String SEARCH_BING				= HTTP + "www.bing.com/search?q=";
	public static final String SEARCH_YOUTUBE			= HTTP + "www.youtube.com/results?search_query=";
	public static final String SEARCH_WIKIPEDIA			= HTTP + "www.wikipedia.org/w/index.php?search=";


	private NamedPattern[] patterns =  new NamedPattern[] {
			NamedPattern.compile("search for ([\\w0-9 ]+)"),
			NamedPattern.compile("search (google|yahoo|bing|youtube|wiki(pedia)?) for ([\\w0-9 ]+)"),
			NamedPattern.compile("(who|what) (is|are) ([\\w0-9 ]+)"),
			NamedPattern.compile("define ([\\w ]+)"),
	};

	@Override
	public boolean execute(MatchResult match, BubbleAdapter adapter) {

		String query;
		String engine;

		switch(match.MatchIndex) {
			case 0:
				query = match.Matcher.group(1);	
				adapter.addItem(new Bubble("Showing google results for \'" + query + "\' ", match));				
				AIActivity.get().ShowWidget(new WebWidget(AIActivity.get(), SEARCH_GOOGLE + Uri.encode(query)));
				break;

			case 1:
				engine = match.Matcher.group(1);
				query = match.Matcher.group(2);			

				StringBuilder sb = new StringBuilder();			
				sb.append("Showing ");
				sb.append(engine);
				sb.append(" results for \'");
				sb.append(query);
				sb.append("\' ");

				adapter.output(sb.toString());
				String header = "";
				if(engine.equals("google"))					header = SEARCH_GOOGLE;
				else if(engine.equals("yahoo"))				header = SEARCH_YAHOO;
				else if(engine.equals("yahoo answers"))		header = SEARCH_YAHOO_ANSWERS;
				else if(engine.contains("wiki"))			header = SEARCH_WIKIPEDIA;
				else if(engine.equals("youtube"))			header = SEARCH_YOUTUBE;

				AIActivity.get().ShowWidget(new WebWidget(AIActivity.get(), header + Uri.encode(query)));					
				break;


			case 2:
				query = match.Matcher.group(3);
				String s = Function.getWikiSniplet(query);
				if(s == null) 	adapter.output("I am unable to recall that right now");
				else 			adapter.output(s);
				break;

				
			case 3:
				//adapter.addItem(new Bubble("Im thinking..."));
				String word = match.Matcher.group(1);
				word = word.trim();
				
				//TODO: find a functional api to get the first paragraph of a Wikipedia page
				
				adapter.output("I don't know how to do that yet");
				break;

				
			default:
				return false;
		}
		return true;
	}
	
	@Override
	public boolean cullFunction(String statement) { 
		return statement.contains("search") |
				statement.contains("define") |
				statement.contains("what") |
				statement.contains("who") |
				statement.contains("when") |
				statement.contains("where") |
				statement.contains("why") |
				statement.contains("how");
	}

	@Override
	public MatchResult isMatch(String statement) { return super.isMatch(statement, patterns, 0.76f); }

}

